package org.ben.birthday.twentybirthday.service.impl;

import org.ben.birthday.twentybirthday.dao.BackGroundImageDaoRepository;
import org.ben.birthday.twentybirthday.entity.BackGroundImage;
import org.ben.birthday.twentybirthday.service.IBackGroundImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @Author: Ben
 * @Date: 2019/8/7 22:09
 */
@Service
public class BackGroundImageImpl implements IBackGroundImageService {

    @Autowired
    BackGroundImageDaoRepository backGroundImageDaoRepository;

    private static final Logger logger = LoggerFactory.getLogger(BackGroundImageImpl.class);


    /**
     * 增加背景图片Service
     * @param backGroundImage
     * @return boolean
     */
    @Override
    public boolean insertBackGroundImageService(BackGroundImage backGroundImage) {
     List<BackGroundImage> backGroundImageList=backGroundImageDaoRepository.findBackGroundImageByPictureUrl(backGroundImage.getPictureUrl());
        Iterator<BackGroundImage> backGroundImageIterator=backGroundImageList.iterator();
        while (backGroundImageIterator.hasNext()){
            logger.error("您插入的图片已存在，插入失败");
            return false;
        }
        backGroundImageDaoRepository.save(backGroundImage);
        logger.info("图片插入成功");
        return true;
    }

    /**
     *删除背景图片Service
     * @param backGroundImageId
     * @return boolean
     */
    @Override
    public boolean deleteBackGroundImageService(Integer backGroundImageId,String pictureUrl) {
       List<BackGroundImage> backGroundImageList=backGroundImageDaoRepository.findBackGroundImageByPictureUrl(pictureUrl);
       Iterator<BackGroundImage> backGroundImageIterator=backGroundImageList.iterator();
       while (backGroundImageIterator.hasNext()){
           backGroundImageDaoRepository.deleteById(backGroundImageId);
           logger.info("删除成功");
           return true;
       }
       logger.error("未找到该图片，删除失败");
        return false;
    }

    /**
     * 更改背景图片Service
     * @param backGroundImage
     * @return boolean
     */
    @Override
    public boolean updateBackGroundImageService(BackGroundImage backGroundImage) {
    List<BackGroundImage> backGroundImageList=backGroundImageDaoRepository.findBackGroundImageByPictureUrl(backGroundImage.getPictureUrl());
    Iterator<BackGroundImage> backGroundImageIterator=backGroundImageList.iterator();
    while (backGroundImageIterator.hasNext()){
        backGroundImageDaoRepository.saveAndFlush(backGroundImage);
        logger.info("图片信息更改成功");
        return true;
    }
    logger.error("未找到该图片，图片信息更改失败");
        return false;
    }

    /**
     * 根据backGroundImageId查询背景图片信息Service
     * @param backGroundImageId
     * @return BackGroundImage
     */
    @Override
    public BackGroundImage selectBackGroundImageService(Integer backGroundImageId) {
        Optional<BackGroundImage> optionalBackGroundImage=backGroundImageDaoRepository.findById(backGroundImageId);
       boolean judgment=optionalBackGroundImage.isPresent();
       if (judgment){
           BackGroundImage backGroundImage=optionalBackGroundImage.get();
           logger.info(backGroundImage.toString());
           return backGroundImage;
       }else {
           logger.error("您查询的图片不存在，请重新查询");
           return null;
       }
    }
}